/// This file cleans up the data downloaded from Qualtrics.

use Study1_raw.dta, clear

*** Drop observations of individuals who did the survey too quickly or took too long.
keep if q_totalduration > 336
keep if q_totalduration < 1993

*** Rename Treatement Variables 
g treatment = 1 if t_control_1 == 1
replace treatment = 2 if t_security_1 == 1
replace treatment = 3 if t_hr_1 == 1
replace treatment = 4 if var65 == 1
replace treatment = 5 if t_imm_1 == 1
replace treatment = 6 if t_dom_1 == 1

tab treatment, gen(treatment2)

*** Create DV Variable
gen r_imm2 = 1 if r_imm <2
replace r_imm2 = 0 if r_imm==3
replace r_imm2 = -1 if r_imm>4

*** Create Demographic Variables
gen white = 1 if race == 1
replace white = 0 if race > 1

gen black = 1 if race == 2
replace black = 0 if race > 2 | race == 1

gen hisp = 1 if race == 3
replace hisp = 0 if race > 3 | race < 3

gen asian = 1 if race == 4
replace asian = 0 if race > 4 | race < 4

gen age2 = age*age

gen pidnew = 0 if var156 == 1 & pid_strong == 1
replace pidnew = 1 if var156 == 1 & pid_strong == 0
replace pidnew = 2 if var156 == 3 & pid_lean == 1
replace pidnew = 2 if var156 == 4 & pid_lean == 1
replace pidnew = 3 if var156 == 3 & pid_lean == 0
replace pidnew = 3 if var156 == 4 & pid_lean == 0
replace pidnew = 4 if var156 == 3 & pid_lean == -1
replace pidnew = 4 if var156 == 4 & pid_lean == -1
replace pidnew = 5 if var156 == 2 & pid_strong == 0
replace pidnew = 6 if var156 == 2 & pid_strong == 1


foreach y of varlist treatment22-treatment26{
	gen pid_`y' = pidnew*`y'
	}

gen female = 1-male

foreach y of varlist treatment22-treatment26{
	gen fem_`y' = female*`y'
	}

	
**** Isolate Immigration Treatment
gen rep =1 if pidnew<3
replace rep = 0 if pidnew >3 & pidnew !=.

foreach y of varlist treatment22-treatment26{
	gen rep_`y' = rep*`y'
	}
	



*** DROPPING UNUSED VARIABLES ***
drop v1 - pid timeloadintro - timeloadtf operation - intro news1 - treat_time_4 ht_man2 - govt_grid_8 ht_time6_1 - jbreader vet - vet_family
drop born_again - var156 finalq - locationaccuracy

	
***Rescaling variables
gen concernR = (concern - 1)/4
gen problemR = (problem-1)/4
gen r_immR = (5-r_imm)/4

gen ageR = (age-18)/68
gen religiosity = 1 if religion != 5
replace religiosity = 0 if religion == 5
gen incomeR = (income-5)/170
gen college = 1 if educ >= 4
replace college = 0 if educ < 4


*** Generating/Rescaling Variables for Moderation
gen treat=0 if treatment==1
replace treat=1 if treatment==5
gen pidnewR = pidnew/6

gen treat_rep = treat*rep

label var treat_rep "Treatment X Republican"


save "Study1_Clean.dta", replace
